<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/12 0012
 * Time: 11:55
 */

namespace app\author\controller;


use think\Controller;

class Book extends Allow
{
    public function index(){
        $member_id = session('author_member_id');
        $book = db("book")->where('m_id',$member_id)->select();
        $this->assign([
            'book' => $book,
            'total' => count($book),
        ]);
//        dump($book);die;
        return view("index");
    }
    public function add(){
        $man = db("cate")->where('pid',1)->select();
        $woman = db("cate")->where('pid',2)->select();
        $chuban = db("cate")->where('pid',3)->select();
        $secondary = db("cate")->where('pid',4)->select();
        $this->assign([
            'man' => $man,
            'woman' => $woman,
            'chuban' => $chuban,
            'secondary' => $secondary
        ]);
        if(request()->post()){
            $name = request()->param('name');
            $cate_id = request()->param('cate_id');
            $descr = request()->param('descr');
//            $cate_name = db("cate")->where('id',$cate_id)->value('name');
            $cate = db("cate")->where('id',$cate_id)->find();
            $pen_name = db("member")->where('id',session("author_member_id"))->value('pen_name');
            $file = request()->file('image');
            if(!empty($file)){
                $info=$file->move( './static/images');
                $filename=str_replace("\\","/",$info->getSaveName());
                $image = 'http://www.hlrork.com/'.'/static/images/'.$filename;
            }else{
                $image = null;
            }
            $data = [
                'name' => $name,
                'cate_id' => $cate_id,
                'cate_name' => $cate['name'],
                'cate_pid' => $cate['pid'],
//                'http://'.$_SERVER['HTTP_HOST'].'/'.
                'cover' => $image,
                'descr' => $descr,
                'author' => $pen_name,
                'm_id' => session("author_member_id")
            ];
            $res = db("book")->insert($data);
            if($res){
                return ['state' => 200,'msg' => 'SUCCESS'];
            }else{
                return ['state' => -1,'msg' => 'ERROR'];
            }
        }
        return view("add");
    }
    public function delete(){
        $id = input("id");
//        $book = db("book")->where("id",$id)->find();
        $del = db("book")->where("id",$id)->delete();
        if($del){
//            unlink('.'.$book['cover']);
            return ['state' => 200,'msg' => 'SUCCESS'];
        }else{
            return ['state' => -1,'msg' => 'ERROR'];
        }
    }
    public function done(){
        $id = input("id");
        $res = db("book")->where('id',$id)->update(['complete' => 1]);
        if($res){
            return ['state' => 200,'msg' => 'SUCCESS'];
        }else{
            return ['state' => -1,'msg' => 'ERROR'];
        }
    }
//    未完成
    public function undone(){
        $id = input("id");
//        $data = db("book")->where('id',$id)->find();
//        dump($data);die;
        $res = db("book")->where('id',$id)->update(['complete' => 0]);
        if($res){
            return ['state' => 200,'msg' => 'SUCCESS'];
        }else{
            return ['state' => -1,'msg' => 'ERROR'];
        }
    }
    //编辑
    public function edit(){
        $id = input("id");
        if(request()->post()){
            $param = request()->param();
            if(isset($param['image'])){
                $image = $param['cover'];
            }else{
                $file = request()->file('image');
                $info=$file->move( './static/images');
                $filename=str_replace("\\","/",$info->getSaveName());
                $image = '/static/images/'.$filename;
            }
//            $cate_name = db("cate")->where('id',$param['cate_id'])->value('name');
            $cate = db("cate")->where('id',$param['cate_id'])->find();
            $data = [
                'name' => $param['name'],
                'cate_id' => $param['cate_id'],
                'cate_name' => $cate['name'],
                'cate_pid' => $cate['pid'],
                'descr' => $param['descr'],
                'cover' => $image,
            ];
            $res = db("book")->where('id',$param['id'])->update($data);
//            return $res;
            if($res){
                return ['state' => 200,'msg' => 'SUCCESS'];
            }else{
                return ['state' => -1,'msg' => 'ERROR'];
            }
        }
        $data = db("book")->find($id);
        $man = db("cate")->where('pid',1)->select();
        $woman = db("cate")->where('pid',2)->select();
        $chuban = db("cate")->where('pid',3)->select();
        $secondary = db("cate")->where('pid',4)->select();
        $this->assign([
            'data' => $data,
            'man' => $man,
            'woman' => $woman,
            'chuban' => $chuban,
            'secondary' => $secondary
        ]);
        return view("edit");
    }
}